//
//  CLLockInfoView.m
//  CoreLock
//
//  Created by 成林 on 15/4/27.
//  Copyright (c) 2015年 冯成林. All rights reserved.
//

#import "CLLockInfoView.h"
#import "CoreLockConst.h"
#import "CLLockItemView.h"

@implementation CLLockInfoView

- (void)drawRect:(CGRect)rect {
    //获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    //设置属性
    CGContextSetLineWidth(ctx, CoreLockArcLineW);
    //设置线条颜色
    [CoreLockCircleLineNormalColor set];
    //新建路径
    CGMutablePathRef pathM = CGPathCreateMutable();
    
    CGFloat marginV = 10.f;
    CGFloat padding = 1.0f;
    CGFloat rectWH = (rect.size.width - marginV * 2 - padding * 2) / 3;
    
    //添加圆形路径
    for (NSUInteger i = 0; i < 9; i ++) {
        NSUInteger row = i % 3;
        NSUInteger col = i / 3;
        CGFloat rectX = (rectWH + marginV) * row + padding;
        CGFloat rectY = (rectWH + marginV) * col + padding;
        CGRect rect = CGRectMake(rectX, rectY, rectWH, rectWH);
        if (self.itemViewsM.count) {
            for (CLLockItemView *itemView in self.itemViewsM) {
                if (i != itemView.tag) {
                    CGPathAddEllipseInRect(pathM, NULL, rect);
                }
            }
        } else {
            CGPathAddEllipseInRect(pathM, NULL, rect);
        }
    }
    
    //添加路径
    CGContextAddPath(ctx, pathM);
    //绘制路径
    CGContextFillPath(ctx);
    //释放路径
    CGPathRelease(pathM);
    
    //新建路径
    CGMutablePathRef pathM1 = CGPathCreateMutable();

    if (self.itemViewsM.count) {
        //设置线条颜色
        [CoreLockCircleLineSelectedCircleColor set];
        //添加圆形路径
        for (NSUInteger i = 0; i < 9; i ++) {
            NSUInteger row = i % 3;
            NSUInteger col = i / 3;
            CGFloat rectX = (rectWH + marginV) * row + padding;
            CGFloat rectY = (rectWH + marginV) * col + padding;
            CGRect rect = CGRectMake(rectX, rectY, rectWH, rectWH);
            for (CLLockItemView *itemView in self.itemViewsM) {
                if (i == itemView.tag) {
                    CGPathAddEllipseInRect(pathM1, NULL, rect);
                }
            }
        }
    }
    
    //添加路径
    CGContextAddPath(ctx, pathM1);
    //绘制路径
    CGContextFillPath(ctx);
    //释放路径
    CGPathRelease(pathM1);
    
//    if (self.itemViewsM.count) {
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [self.itemViewsM removeAllObjects];
//            [self setNeedsDisplay];
//        });
//    }
}

#pragma mark - Lazy

- (NSMutableArray *)itemViewsM {
    if (!_itemViewsM) {
        _itemViewsM = [[NSMutableArray alloc] init];
    }
    
    return _itemViewsM;
}

@end
